<?php 
/*
Template Name: process Product
*/
?>
<?php get_header(); ?>
<?php 
    if ( !is_user_logged_in() ) 
    {
      ?>

        
              <?php wp_die( __( 'Vui lòng đăng nhập hoặc quay lai trang chủ ! ' ) ); ?>
      
         
     <?php 
          
    }//if ( ! current_user_can( 'list_users' ) ) 
    elseif (is_user_logged_in() )  
 { 

    if ( ( $current_user->roles[0] == 'author') or ( $current_user->roles[0] == 'contributor') or ( $current_user->roles[0] == 'subscriber') ) 
      {
       ?>

        <div id='content_nano_two' class='clearfix' style='min-height:300px;'>
              <h1 style='color:#000;'>bạn không được phép truy cập trang này </h1>
              <?php wp_die( __( 'Vui lòng đăng nhập hoặc quay lai trang chủ ! ' ) ); ?>
        </div>
         
     <?php 

      }//if ( ( $current_user->roles[0] == 'author') or ( $current_user->roles[0] == 'contributor') or ( $current_user->roles[0] == 'subscriber') ) 
      elseif ( ( $current_user->roles[0] != 'author') or ( $current_user->roles[0] != 'contributor') or ( $current_user->roles[0] != 'subscriber') )
      {
     ?>


		<div id="content_nano_three" class="clearfix" >


		    <?php  
		 //  echo '$_POST[name_may_edit] :'.$_POST['name_may_edit'];
		 //  echo '$_POST[id_may] : '.$_POST['id_may'];

		    if (isset($_POST['name_may_edit']) and isset($_POST['id_may'])) 
		    {
		    	if (isset($_POST['ImagePath'])) { $images = $_POST['ImagePath']; }
		    	if($images <>'') 
		    	 {
							$phuc = explode( '/', $images);
							//var_dump($phuc);
							//echo $phuc[7] ;
							$images_url = $phuc[7];
				  }else  $images_url = $_POST['ImagePath'];
		    ?>
		        

		        <div id="breaum_suausser"><h2>Sửa thông tin máy</h2></div>
				<div id="content" class="edit_user-page">
		                <div id='edit_may'>
								<?php
								$ngay_sua = date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) );
								?>
								<?php
								
									if (strlen($_POST['name_may_edit']) <> '')
									{
										

										
										$wpdb->replace( 
											'wp_may', 
											array( 
											    'id_may' => $_POST['id_may'],
												'name_may' => $_POST['name_may_edit'], 
												'image_may_url' => $images_url,
												'ngay_tao' => $_POST['ngay_tao'],
												'ngay_sua' => $ngay_sua,
											), 
											array( 
												'%d', 
												'%s', 
												'%s',
												'%s',
												'%s'
											) 
										);


										// set quyen cho admin duoc quan lí may nay 

										global $current_user;
										get_currentuserinfo();
										$id_admin_now =  $current_user->ID;

										//$sql = 'SELECT * FROM wp_may order by id_may desc';
										//$id_may_array = $wpdb->get_row("SELECT * FROM wp_may order by id_may desc", ARRAY_N);

										//var_dump($id_may_array[0]);

										$wpdb->insert( 
	                                        'wp_user_wp_may', 
	                                        array( 
	                                          'ID' => $id_admin_now, 
	                                          'id_may' => $_POST['id_may'],
	                                        ), 
	                                        array( 
	                                          '%d', 
	                                          '%d'
	                                        ) 
	                                        );
									
									echo "<h1>Sửa thành công</h1>";
									?>
									<a href="<?php bloginfo('url');?>/quan-li-may"><button class="button_del">quay lại</button></a>
									<?php
									}else {
										echo "<h1>Chưa nhâp tên máy </h1>";
										?>
										<a href="<?php bloginfo('url');?>/quan-li-may"> <button class="button_del">quay lại</button></a>
										<?php 
									}
						        ?> 	
		                </div><!-- end div_form_edit-->
		         </div><!--- id="content" class="edit_user-page" -->
	     <?php 
		    }//if (isset($_POST['name_may_edit']))
		    ?>


		     <?php  
		   // echo '$_POST[name_may]'.$_POST['name_may'];

		   if (isset($_POST['name_may']) or isset($_POST['upload'])) 
		    {
		    	if (isset($_POST['ImagePath'])) { $images = $_POST['ImagePath']; }
		    	if($images <>'') 
		    	 {
							$phuc = explode( '/', $images);
							//var_dump($phuc);
							//echo $phuc[7] ;
							$images_url = $phuc[7];
				  }else  $images_url = $_POST['ImagePath'];
		    ?>

		        <div id="breaum_suausser">
		          <h2>Thêm máy mới</h2>
		          
		        </div>

				<div id="content" class="edit_user-page">

		                <div id='edit_may'>

								<?php
								$ngay_tao = date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) );
								$ngay_sua = date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) );
								?>
								<?php
								
								
									if (strlen($_POST['name_may']) <> '')
									{
										$wpdb->insert( 
											'wp_may', 
											array( 
												'name_may' => $_POST['name_may'], 
												'image_may_url' => $images_url,
												'ngay_tao' => $ngay_tao,
												'ngay_sua' => $ngay_sua,
											), 
											array( 
												'%s', 
												'%s',
												'%s',
												'%s'
											) 
										);

										// set quyen cho admin duoc quan lí may nay 

										global $current_user;
										get_currentuserinfo();
										$id_admin_now =  $current_user->ID;

										//$sql = 'SELECT * FROM wp_may order by id_may desc';
										$id_may_array = $wpdb->get_row("SELECT * FROM wp_may order by id_may desc", ARRAY_N);

										//var_dump($id_may_array[0]);


										


										$wpdb->insert( 
	                                        'wp_user_wp_may', 
	                                        array( 
	                                          'ID' => $id_admin_now, 
	                                          'id_may' => $id_may_array[0],
	                                        ), 
	                                        array( 
	                                          '%d', 
	                                          '%d'
	                                        ) 
	                                        );




									echo "<h1>Thêm thành công</h1>";
									?>
									<a href="<?php bloginfo('url');?>/quan-li-may"><button class="button_del" >quay lại</button></a>
									<?php
									}else {
										echo "<h1>Chưa nhâp tên máy </h1>";
										?>
										<a href="<?php bloginfo('url');?>/quan-li-may"><button class="button_del" >quay lại</button></a>
										<?php 
									}
									
						        ?> 	
		                </div><!-- end div_form_edit-->
		        </div><!--- id="content" class="edit_user-page" -->
		 <?php 
			    }//if (isset($_POST['name_may']) or isset($_POST['upload'])) 
			    ?>



		     
		</div><!-- id="content_nano_three" -->

<?php 
		} //  elseif ( ( $current_user->roles[0] != 'author') or ( $current_user->roles[0] != 'contributor') or ( $current_user->roles[0] != 'subscriber') )
} //elseif ( ! current_user_can( 'list_users' ) )
		?>
<?php get_footer(); ?>